<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable width="70%">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" style="height:100px;" label-width="120px" @keyup.enter="submitHandle()">
			<div class="divline">
				<el-form-item prop="clueName" label="名称">
					<el-input v-model="dataForm.clueName" placeholder="名称"></el-input>
				</el-form-item>
			</div>
		</el-form>
		<template #footer>
			<div class="divbtn">
				<el-button @click="visible = false">取消</el-button>
				<el-button  v-auth="'sys:cluesource:save'" type="primary" @click="submitHandle()">确定</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import {useClueSourceSubmitApi,useClueSourceApi} from '@/api/sys/cluesource'
const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
	id: '',
	clueName:'',
})
const init = (obj:any) => {
	visible.value = true
	dataForm.id = '';
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	// id 存在则为修改
	if (obj) {
		getClueSource(obj.id);
	}
}
// 获取信息
const getClueSource = (id: number) => {
	useClueSourceApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}
const datalistUserRef = ref()
const addOrUpdateDeptHandle = () => {
	datalistUserRef.value.init();
}
const dataRules = ref({
	clueTitle: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	clueArea: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	clueName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	cluTel: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useClueSourceSubmitApi(dataForm).then((res:any) => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>


<style lang="scss" scoped>
.divline {
	width: 96%;
	height: 30px;
	float: left;
	margin-top: 20px;
}
.divfileline{
	width: 96%;
	float: left;
	margin-left: 40px;
	margin-top: 40px;
}
.divlineleft{
	width: 47%;
	float: left;
	height: 30px;
	padding-left: 10px;
}

.divlinecenter{
	width: 95.5%;
	float: left;
	height: 30px;
	padding-left: 10px;
}
.divbtn{
	text-align: center;
	margin-top: 20px;

}
.eduline{
	width: 96%;
	float: left;
	height: 50px;
	margin-top: 15px;
	border-bottom: 1px solid #BEBEBE;
	border-top: 1px solid #BEBEBE;
}
.divsplit{
	width: 96%;
	float: left;
	height: 1px;
	background-color:#409eff;
    margin-top: 5px;
}
.divsplit:last-child{
	width: 96%;
	float: left;
	height: 1px;
}
.eduitem{
	width: 20%;
	height: 30px;
	float: left;
    padding-top: 10px;
}
.inputVal.el-input__wrapper{
	width: 60%;
	float: left;
	height: 30px;
}
.inputVal.el-input{
	width: 60%;
	float: left;
	height: 30px;
}
.texttit{
	width: 30%;
	float: left;
	height: 30px;
	line-height: 2;
}
.eduitembtn{
	
	width: 19%;
	height: 30px;
	float: left;
    padding-top: 10px;
}
</style>
